package com.example.lib_mvp;

import android.app.Activity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lib_mvp.adapter.PopMusicListAdapter;
import com.example.lib_mvp.adapter.TransFaceLangAdapter;
import com.example.lib_mvp.beans.HdTransDocxBean;
import com.example.lib_mvp.views.RippleSearchView;
import com.hjq.window.EasyWindow;

import com.tjf.lib_utils.LogsUtils;
import com.tjf.lib_utils.PxUtils;

import java.util.List;

public class TipUiDialog {
    static EasyWindow easyWindow;


    public static void showFwBhXy(Activity context, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_server_yinsi_view);
        easyWindow.setBackgroundDimAmount(0.2f);
        easyWindow.setGravity(Gravity.CENTER);
        easyWindow.show();
        easyWindow.findViewById(R.id.tip_cancel_tv).setOnClickListener(v -> dismiss());
        easyWindow.findViewById(R.id.tip_confirm_tv).setOnClickListener(onClickListener);
    }

    public static void showLxKf(Activity context) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_linanxikefu_view);
        easyWindow.setGravity(Gravity.CENTER);
        easyWindow.setBackgroundDimAmount(0.6f);
        actionOutsideTouch();
        easyWindow.show();
        TextView app_version_tv = (TextView) easyWindow.findViewById(R.id.app_version_tv);
//        app_version_tv.setText("版本号：" + BuildConfig.VERSION_NAME);
    }

    public static void showOutLogin(Activity context, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_out_login_view);
        easyWindow.setGravity(Gravity.CENTER);
        easyWindow.setOutsideTouchable(false);
        easyWindow.setBackgroundDimAmount(0.6f);
        easyWindow.setDuration(10 * 1000);
        easyWindow.show();
        easyWindow.findViewById(R.id.tip_cancel_tv).setOnClickListener(v -> dismiss());
        easyWindow.findViewById(R.id.tip_confirm_tv).setOnClickListener(v -> {
            dismiss();
            onClickListener.onClick(v);
        });
    }

    public static void showHfSz(Activity context, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_hfcc_sz_view);
        easyWindow.setGravity(Gravity.CENTER);
        easyWindow.setOutsideTouchable(false);
        easyWindow.setBackgroundDimAmount(0.6f);
        easyWindow.setDuration(10 * 1000);
        easyWindow.show();
        easyWindow.findViewById(R.id.tip_cancel_tv).setOnClickListener(v -> dismiss());
        easyWindow.findViewById(R.id.tip_confirm_tv).setOnClickListener(v -> {
            dismiss();
            onClickListener.onClick(v);
        });
    }

    public static void showCloseAccount(Activity context, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_close_account_view);
        easyWindow.setGravity(Gravity.CENTER);
        easyWindow.setBackgroundDimAmount(0.6f);
        easyWindow.setOutsideTouchable(false);
        easyWindow.setDuration(10 * 1000);
        easyWindow.show();
        easyWindow.findViewById(R.id.tip_cancel_tv).setOnClickListener(v -> dismiss());
        easyWindow.findViewById(R.id.tip_confirm_tv).setOnClickListener(v -> {
            dismiss();
            onClickListener.onClick(v);
        });
    }

    public static void showMusicList(Activity context, int descIcon, String mListDesc, PopMusicListAdapter popMusicListAdapter, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_pop_music_list);
        easyWindow.setBackgroundDimAmount(0.6f);
        easyWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER);
        actionOutsideTouch();
        easyWindow.show();


        ImageView pop_music_mode_name_img = (ImageView) easyWindow.findViewById(R.id.pop_music_mode_name_img);
        pop_music_mode_name_img.setImageResource(descIcon);
        TextView pop_music_mode_name_tv = (TextView) easyWindow.findViewById(R.id.pop_music_mode_name_tv);

        pop_music_mode_name_tv.setText(mListDesc);
        easyWindow.findViewById(R.id.pop_del_img).setOnClickListener(v -> dismiss());

        RecyclerView musicRvList = (RecyclerView) easyWindow.findViewById(R.id.pop_mode_type_rvlist);
        musicRvList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        musicRvList.setAdapter(popMusicListAdapter);


    }

    public static void showDeleteRecord(Activity context, int tipStatus, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_delect_record_view);
        easyWindow.setGravity(Gravity.CENTER);
        easyWindow.setBackgroundDimAmount(0.6f);
        easyWindow.setOutsideTouchable(false);
        easyWindow.setDuration(10 * 1000);
        actionOutsideTouch();
        easyWindow.show();
        TextView tip_content_tv = (TextView) easyWindow.findViewById(R.id.tip_content_tv);
        String content = null;
        if (tipStatus == 0) {
            content = "是否要删除该条录音记录";
        } else if (tipStatus == 1) {
            content = "是否要删除该条翻译记录";
        }
        tip_content_tv.setText(content);
        easyWindow.findViewById(R.id.tip_cancel_tv).setOnClickListener(v -> dismiss());
        easyWindow.findViewById(R.id.tip_confirm_tv).setOnClickListener(v -> {
            dismiss();
            onClickListener.onClick(v);
        });
    }

    /**
     * 蓝牙扫描 及列表框
     *
     * @param context
     * @param
     */
    public static void showBluetoothList(Activity context) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_search_bluether_view);
        easyWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER);
        easyWindow.setOutsideTouchable(true);
        easyWindow.setHeight(PxUtils.dp2px(context, 820));
        actionOutsideTouch();
        easyWindow.show();
        View contentView = easyWindow.getContentView();


        RippleSearchView mRippleSearchView = contentView.findViewById(R.id.ripple_search_view);

        // 开始波纹动画
        mRippleSearchView.startRippleAnimation();


        ImageView pop_del_img = contentView.findViewById(R.id.pop_del_img);
        pop_del_img.setOnClickListener(v -> {
            dismiss();
            mRippleSearchView.stopRippleAnimation();
        });

    }

    /**
     * 语言选择框
     *
     * @param context
     * @param onClickListener
     */
    public static void showLanguage(Activity context, int selectPosition,
                                    List<HdTransDocxBean.DataDTO> hdTransDocBeanList,
                                    onLangDuListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_select_lang_view);
//        easyWindow.setGravity(Gravity.CENTER);
        //        easyWindow.setOutsideTouchable(false);
//        easyWindow.setDuration(10 * 1000);
//        actionOutsideTouch();
        easyWindow.show();
        View contentView = easyWindow.getContentView();
        contentView.findViewById(R.id.pop_dialog_dismiss_img)//返回
                .setOnClickListener(v -> dismiss());
        RecyclerView pop_trans_lang_rvlist = contentView.findViewById(R.id.pop_lang_rvlist);
        pop_trans_lang_rvlist.setLayoutManager(new LinearLayoutManager(context));
        for (HdTransDocxBean.DataDTO dao : hdTransDocBeanList) {
            dao.setSelect(false);
        }
        hdTransDocBeanList.get(selectPosition).setSelect(true);
        LogsUtils.i("TRANSLATE_DOCX==3=有数据了", hdTransDocBeanList.size());

        TransFaceLangAdapter transLangAdapter =
                new TransFaceLangAdapter(R.layout.hd_pop_trans_lang_item, hdTransDocBeanList);
        pop_trans_lang_rvlist.setAdapter(transLangAdapter);
        transLangAdapter.setOnItemClickListener((adapter, view, position) -> {
            for (HdTransDocxBean.DataDTO dao : hdTransDocBeanList) {
                dao.setSelect(false);
            }
            hdTransDocBeanList.get(position).setSelect(true);
            HdTransDocxBean.DataDTO dto = hdTransDocBeanList.get(position);
            if (onClickListener != null) {
                onClickListener.onClickItemData(position, dto);
            }
         /*   if (langType == 1) {
                fromTrans = dto.getDst();
                fromCode = dto.getLangid();
                from_lang = dto.getLang();

                fromLange = dto.getLanguage();

            } else if (langType == 2) {

                toTrans = dto.getDst();
                toCode = dto.getLangid();
                toLange = dto.getLanguage();
                to_lang = dto.getLang();
            }
*/
            dismiss();
        });
    }

    public interface onLangDuListener {
        void onClickItemData(int type, HdTransDocxBean.DataDTO LangDto);
    }

    /**
     * 翻译 的功能选择
     *
     * @param context
     */
    public static void showLongTransGn(Activity context, View anchorView,
                                       View.OnClickListener onLangDuListener,
                                       View.OnClickListener onCopyListener, View.OnClickListener onDelectListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_trans_select_tip_view);
//        easyWindow.setGravity(Gravity.CENTER);
//        easyWindow.setBackgroundDimAmount(0.2f);
//        easyWindow.setOutsideTouchable(false);
//        easyWindow.setDuration(10 * 1000);
        actionOutsideTouch();
        // easyWindow.show();
        easyWindow.showAsDropDown(anchorView, Gravity.TOP, 0, PxUtils.dp2px(context, 38));
        View contentView = easyWindow.getContentView();

        LinearLayout pop_langdu_lly = contentView.findViewById(R.id.pop_langdu_lly);
        LinearLayout pop_copy_lly = contentView.findViewById(R.id.pop_copy_lly);
        LinearLayout pop_delect_lly = contentView.findViewById(R.id.pop_delect_lly);

        pop_langdu_lly.setOnClickListener(v -> {
            onLangDuListener.onClick(v);
            dismiss();
        });
        pop_copy_lly.setOnClickListener(v -> {
            onCopyListener.onClick(v);
            dismiss();
        });
        pop_delect_lly.setOnClickListener(v -> {
            dismiss();
            onDelectListener.onClick(v);
        });
    }

    /**
     * 翻译 的功能选择
     *
     * @param context
     */
    public static void showLongAiText(Activity context, View anchorView,
                                      View.OnClickListener onChongxinListener,
                                      View.OnClickListener onCopyListener, View.OnClickListener onDelectListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_ai_text_tip_view);
//        easyWindow.setGravity(Gravity.CENTER);
//        easyWindow.setBackgroundDimAmount(0.2f);
//        easyWindow.setOutsideTouchable(false);
//        easyWindow.setDuration(10 * 1000);
        actionOutsideTouch();
        // easyWindow.show();

        easyWindow.showAsDropDown(anchorView, Gravity.CENTER, PxUtils.dp2px(context, 108),   PxUtils.dp2px(context, 18));

        View contentView = easyWindow.getContentView();

        LinearLayout pop_regenerate_lly = contentView.findViewById(R.id.pop_regenerate_lly);
        LinearLayout pop_copy_lly = contentView.findViewById(R.id.pop_copy_lly);
        LinearLayout pop_delect_lly = contentView.findViewById(R.id.pop_delect_lly);

        pop_regenerate_lly.setOnClickListener(v -> {
            onChongxinListener.onClick(v);
            dismiss();
        });
        pop_copy_lly.setOnClickListener(v -> {
            onCopyListener.onClick(v);
            dismiss();
        });
        pop_delect_lly.setOnClickListener(v -> {
            dismiss();
            onDelectListener.onClick(v);
        });
    }

    public static void showPermission(Activity context, String tip, String mContentDesc, View.OnClickListener onClickListener) {
        dismiss();
        easyWindow = EasyWindow.with(context)
                .setContentView(R.layout.hd_dialog_check_permissio_view);
        easyWindow.setBackgroundDimAmount(0.6f);
        easyWindow.setGravity(Gravity.CENTER);
        actionOutsideTouch();
        easyWindow.show();
        TextView permission_tip = (TextView) easyWindow.findViewById(R.id.permission_tip);
        permission_tip.setText(tip);
        TextView permission_content = (TextView) easyWindow.findViewById(R.id.permission_content);
        permission_content.setText(mContentDesc);
        easyWindow.findViewById(R.id.tip_cancel_tv).setOnClickListener(v -> dismiss());
        easyWindow.findViewById(R.id.tip_confirm_tv).setOnClickListener(v -> {
            onClickListener.onClick(v);
            dismiss();
        });
    }

    /**
     * 点击外部区域消失
     */
    private static void actionOutsideTouch() {
        easyWindow.addWindowFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
        easyWindow.setOnTouchListener((EasyWindow.OnTouchListener<View>) (window, view, event) -> {
            LogsUtils.i("ces ============" + event.getAction());
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                dismiss();
                return true;
            }
            return false;
        });
    }

    public static void dismiss() {
        if (easyWindow != null) {
            easyWindow.cancel();
        }
    }
}
